﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class FND_BRND_CT_PRD_VAR : RibbonForm
    {
        public FND_BRND_CT_PRD_VAR()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }

        public string BrandID { get; set; }
        public string BrandName { get; set; }
        public string ProdctCode { get; set; }
        public string ProdctName { get; set; }

        public string CatCode { get; set; }
        public string CatName { get; set; }
        
        public string VariantCode { get; set; }
        public string VariantName { get; set; }

        void BindGrid()
        {
            ProductVariantManager oManager = new ProductVariantManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable table = oManager.GetVariantByName(this.BrandID, this.CatCode, this.ProdctCode, txtSearch.Text);

            oManager.Close();

            gridControl1.DataSource = table;
            gridControl1.Refresh();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FND_BRND_CT_PRD_VAR_Load(object sender, EventArgs e)
        {
            txtBrandName.Text = this.BrandName;
            txtProduct.Text = this.ProdctName;
            txtCategory.Text = this.CatName;
        }

        private void txtSearch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            BindGrid();
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            int iRowIndex = gridView1.FocusedRowHandle;
            this.VariantCode = gridView1.GetRowCellValue(iRowIndex, gridView1.Columns["VrntCode"]).ToString();
            this.VariantName = gridView1.GetRowCellValue(iRowIndex, gridView1.Columns["VrntName"]).ToString();

            this.Close();
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BindGrid();
            }

            if (e.KeyCode == Keys.Tab)
            {
                BindGrid();
            }
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }

            if (e.KeyCode == Keys.Tab)
            {
                btnChoose_Click(sender, e);
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }
    }
}
